Page 6 of 6

Re: Programming and Computing with Python Right from the Forum

Posted: Tue Aug 16, 2022 5:42 pm
by Eli
Testing Sample Module

  1. import pandas as pd
  2. #import random and use it as random.sample
  3. from random import sample
  4.  
  5. #Let us create some data using sample from random module
  6. #Create two lists
  7. country_list = ["USA", "Russia","China","India"]
  8.  
  9. #Using the name list, let us create three variables using sample() function
  10. top1 = sample(country_list,4)
  11. top2 = sample(country_list,4)
  12. top3 = sample(country_list,4)
  13.  
  14. #Now, we can use these lists to create a dataframe with 3 columns
  15. df = pd.DataFrame({"Top1":top1,
  16.               "Top2":top2,
  17.               "Top3":top3,
  18.              })
  19. print(df)


Re: Live Programming and Computing

Posted: Tue May 30, 2023 9:30 pm
by Eli
Here is a SnapPy, what is it ?

SnapPy is a program for studying the topology and geometry of 3-manifolds, with a focus on hyperbolic structures. It runs on Mac OS X, Linux, and Windows, and combines a link editor and 3D-graphics for Dirichlet domains and cusp neighborhoods with a powerful command-line interface based on the Python programming language.

SnapPy can be installed on Linux as follows:

Ubuntu/Debian/Mint: Tested on Ubuntu 20.04:

  1. sudo apt-get install python3-tk python3-pip
  2. # Note no "sudo" on the next one!
  3. python3 -m pip install --upgrade --user snappy

Users of Ubuntu 18.04 or older should do:

  1. sudo apt-get install python3-tk python3-pip
  2. # Note no "sudo" on the next two
  3. python3 -m pip install --upgrade --user pip wheel
  4. python3 -m pip install --upgrade --user snappy


On TSSFL Stack, SnapPy is installed and can be used via SageMath. We can at once import the whole Stack of SnapPy modules and start testing it immediatelly:

  1. from snappy import Manifold, Triangulation, Manifold, ManifoldHP, AbelianGroup, FundamentalGroup, HolonomyGroup, HolonomyGroupHP, DirichletDomain, DirichletDomainHP, CuspNeighborhood, CuspNeighborhoodHP, SymmetryGroup, AlternatingKnotExteriors, NonalternatingKnotExteriors, SnapPeaFatalError, InsufficientPrecisionError, pari, twister, OrientableCuspedCensus, NonorientableCuspedCensus, OrientableClosedCensus, NonorientableClosedCensus, LinkExteriors, CensusKnots, HTLinkExteriors, TetrahedralOrientableCuspedCensus, TetrahedralNonorientableCuspedCensus, OctahedralOrientableCuspedCensus, OctahedralNonorientableCuspedCensus, CubicalOrientableCuspedCensus, CubicalNonorientableCuspedCensus, DodecahedralOrientableCuspedCensus, DodecahedralNonorientableCuspedCensus, IcosahedralNonorientableClosedCensus, IcosahedralOrientableClosedCensus, CubicalNonorientableClosedCensus, CubicalOrientableClosedCensus, DodecahedralNonorientableClosedCensus, DodecahedralOrientableClosedCensus, Crossing, Strand, Link, Tangle, RationalTangle, ZeroTangle, InfinityTangle, IdentityBraid, random_link, DTcodec
  2.  
  3. A = AbelianGroup(elementary_divisors=[5,15,0,0])
  4. print(A)
  5. print(A[0])
  6.  
  7. M = Manifold('m004')
  8. print(M.symmetry_group())
  9.  
  10. M = Manifold('K7_1')
  11. G = M.fundamental_group()
  12. g = G.generators_in_originals()
  13. print(g)
  14. #M.inside_view()

To explore SnapPy Manifold class for example (unfortunately tinker is not installed on SageMath), open Ubuntu LInux Terminal and evoke Python/Ipython interpreter, import Manifold and execute the below simple code:

  1. from snappy import Manifold
  2. M = Manifold('m004')
  3. M.inside_view()
  4. %gui tk
  5.  
  6. #Code 2
  7. from snappy import ManifoldHP
  8. M = ManifoldHP('14n12345') #Try M = ManifoldHP('15n4321')
  9. M.volume()
  10. M.plink()
  11. M.browse()


Re: Live Programming and Computing

Posted: Fri Jul 07, 2023 12:06 pm
by Eli

Re: Live Programming and Computing

Posted: Mon Sep 18, 2023 7:01 pm
by Eli
  1. import pandas as pd
  2. import re
  3.  
  4. # Sample data
  5. data = {'date_time': ['Random text 2023-09-03 00:20:00 more text', 'Some other text', '2023-09-03 00:20:00 additional text']}
  6. df = pd.DataFrame(data)
  7.  
  8. # Extract the first datetime occurrence with the desired format
  9. pattern = r'\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}'
  10. first_datetime = re.search(pattern, ' '.join(df['date_time']))
  11.  
  12. if first_datetime:
  13.     # Extract the time from the first datetime occurrence
  14.     first_time = first_datetime.group().split()[1]
  15.     print(first_time)
  16. else:
  17.     print("Datetime not found in DataFrame.")


Re: Live Programming and Computing with Python, R, Sage, Octave, Maxima, Singular, Gap, GP, HTML & Macaulay2

Posted: Tue Dec 05, 2023 5:31 pm
by Eli
Python Polars is an alternative to Pandas, written with performance in mind. Test it:

  1. import polars as pl
  2.  
  3. from datetime import datetime
  4.  
  5. df = pl.DataFrame(
  6.     {
  7.         "integer": [1, 2, 3, 4, 5],
  8.         "date": [
  9.             datetime(2023, 1, 1),
  10.             datetime(2023, 1, 2),
  11.             datetime(2023, 1, 3),
  12.             datetime(2023, 1, 4),
  13.             datetime(2023, 1, 5),
  14.         ],
  15.         "float": [4.0, 5.0, 6.0, 7.0, 8.0],
  16.     }
  17. )
  18.  
  19. print(df)
  20.  
  21. print(df.sample(2))
  22. print(df.describe())


Re: Live Programming and Computing with Python, R, Sage, Octave, Maxima, Singular, Gap, GP, HTML & Macaulay2

Posted: Fri Jan 19, 2024 8:36 am
by Eli
Test Vega Altair

  1. # import altair with an abbreviated alias
  2. import altair as alt
  3.  
  4. # load a sample dataset as a pandas DataFrame
  5. from vega_datasets import data
  6. cars = data.cars()
  7.  
  8. # make the chart
  9. chart = alt.Chart(cars).mark_point().encode(
  10.     x='Horsepower',
  11.     y='Miles_per_Gallon',
  12.     color='Origin',
  13. ).interactive()
  14.  
  15. chart.save('chart.html') # Save to file


Re: Live Programming and Computing with Python, R, Sage, Octave, Maxima, Singular, Gap, GP, HTML & Macaulay2

Posted: Sat Jul 20, 2024 4:17 pm
by Eli
These libraries are currently available, test:

  1. import langserve
  2. import openai
  3. import langchain
  4. import langchain_openai
  5. import sklearn
  6. import fastapi
  7. import flask


Re: Live Programming and Computing with Python, R, Sage, Octave, Maxima, Singular, Gap, GP, HTML & Macaulay2

Posted: Sat May 03, 2025 10:36 pm
by Eli
Use R and Python together - read data into R dataframe and then convert it to Pandas Dataframe:

  1. import pandas as pd
  2. from rpy2 import robjects
  3. from rpy2.robjects import pandas2ri
  4.  
  5. #Read data in R
  6. robjects.r('data <- read.csv("https://raw.githubusercontent.com/selva86/datasets/master/mtcars.csv")')
  7.  
  8. #Get the R data frame
  9. r_data = robjects.r['data']
  10.  
  11. #Convert to pandas DataFrame
  12. data_df = pandas2ri.rpy2py(r_data)
  13.  
  14. #Display the DataFrame
  15. print(data_df.head())