LinuxQuestions.org
Help answer threads with 0 replies.
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 06-25-2024, 12:56 PM   #1
piotrbujakowski
Member
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 47

Rep: Reputation: 1
Linux, Python, C ++, Brain2 and errors.


Hi.

I try to make and run first time AI Neural Network on my computer.
Knowledge Python is basic.

I'm using Linux Ubuntu.

I install whole in system using apt.
When show communicate about pip, then I'm doing that.
[Is a lot of problems with right file names and compatibility.]

I try to configure to direction in home catalogue [advice from Gemini].
Port 65432 is unblocked in firewall.

Helpful is Gemini, but can't solve problems what I have now.

Please help me with that.

Python engine:

Code:
import brian2
import socket
import numpy as np

from brian2 import Neuron, Eq


# Define neuron equations
eqs = '''
dv/dt = (I - gl*(v-Vl) - gNa*(m**3)*h*(v-VNa) - gK*(n**4)*(v-VK)) / Cm
dm/dt = alpha_m(v)*(1-m) - beta_m(v)*m
dh/dt = alpha_h(v)*(1-h) - beta_h(v)*h
dn/dt = alpha_n(v)*(1-n) - beta_n(v)*n
'''

# Define parameters and functions for the equations (replace with actual definitions)
Vl = -65*mV  # Leak reversal potential
VNa = 50*mV  # Sodium reversal potential
VK = -70*mV  # Potassium reversal potential
Cm = 1*uF/cm2  # Membrane capacitance
gl = 0.3e-3*siemens/cm2  # Leak conductance
gNa = 120e-3*siemens/cm2  # Sodium conductance
gK = 36e-3*siemens/cm2  # Potassium conductance

# Define neuron object using custom equations
neurons = Neuron(eqs=eqs, methods={'alpha_m': alpha_m, 'beta_m': beta_m, 'alpha_h': alpha_h, 'beta_h': beta_h, 'alpha_n': alpha_n, 'beta_n': beta_n})  # Include all required methods


# Standard variables
num_neurons = 1000
duration = 1000  # Simulation duration in milliseconds

# **(Replace with your implementation)**
# Hodgkin-Huxley neuron model with STDP learning (replace with your specific neuron and synapse definitions)
# ... (Include your specific code for defining synapses and learning rules)


# Data variables
average_firing_rates = []
average_synaptic_weights = []

# Recording functions
def record_firing_rates():
    global average_firing_rates
    average_firing_rates.append(np.mean(spike_monitor.count / (duration * 1000)))  # Convert to Hz

def record_synaptic_weights():
    global average_synaptic_weights
    weights = synapses.weight  # Assuming you have a 'synapses' object with weight attribute
    average_synaptic_weights.append(np.mean(weights))

# Network monitors
spike_monitor = brian2.SpikeMonitor(source=neurons)
brian2.NetworkOperation(record_firing_rates, dt=10*brian2.ms)  # Record every 10 ms
brian2.NetworkOperation(record_synaptic_weights, dt=100*brian2.ms)  # Record every 100 ms

# Network socket setup (replace with your IP address and port)
HOST = '127.0.0.1'  # Standard loopback interface address (localhost)
PORT = 65432        # Port to listen on (non-privileged ports are > 1023)
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind((HOST, PORT))

# Run the simulation
brian2.run(duration * brian2.ms)

# Send data to C++ program (replace with IPC if on the same machine)
data = {'firing_rates': average_firing_rates, 'synaptic_weights': average_synaptic_weights}
data_str = str(data)  # Convert data to string for sending
s.sendall(data_str.encode())

# Close the socket
s.close()
I have too second code in C++:

Code:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sstream>
#include <jsoncpp/json.h>  // Include JSON library for parsing

int main() {
  // Standard variables
  int port = 65432;  // Port used by Brian 2 simulation
  std::string host = "localhost";  // Replace with IP address of Brian 2 (if not localhost)

  // Socket setup
  int sockfd;
  struct sockaddr_in servaddr;

  sockfd = socket(AF_INET, SOCK_STREAM, 0);
  if (sockfd == -1) {
    perror("socket creation failed");
    exit(EXIT_FAILURE);
  }

  memset(&servaddr, 0, sizeof(servaddr));

  servaddr.sin_family = AF_INET;
  servaddr.sin_port = htons(port);
  servaddr.sin_addr.s_addr = inet_addr(host.c_str());

  if (connect(sockfd, (struct sockaddr*)&servaddr, sizeof(servaddr)) != 0) {
    perror("connection failed");
    exit(EXIT_FAILURE);
  }

  // Data receiving and processing loop
  while (1) {
    char buffer[1024]; // Adjust buffer size based on data volume
    int n = recv(sockfd, buffer, sizeof(buffer), 0);
    if (n == 0) {
      printf("Connection closed by server\n");
      break;
    } else if (n == -1) {
      perror("recv failed");
      exit(EXIT_FAILURE);
    }

    // Parse received data (assuming JSON format)
    std::string data_str(buffer, n);
    Json::Reader reader;
    Json::Value data;
    if (!reader.parse(data_str, data)) {
Error:

Code:
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.

= RESTART: /home/peter/python/ai_neural_brain_2/engine_brain_2.py =
ERROR      Brian 2 encountered an unexpected error. If you think this is a bug in Brian 2, please report this issue either to the discourse forum at <http://brian.discourse.group/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: /tmp/brian_debug_vkzoele7.log  Additionally, you can also include a copy of the script that was run, available at: /tmp/brian_script_fgiuug_k.py Thanks! [brian2]
Traceback (most recent call last):
  File "/usr/lib/python3.12/idlelib/run.py", line 580, in runcode
    exec(code, self.locals)
  File "/home/peter/python/ai_neural_brain_2/engine_brain_2.py", line 5, in <module>
    from brian2 import Neuron, Eq
ImportError: cannot import name 'Neuron' from 'brian2' (/usr/lib/python3/dist-packages/brian2/__init__.py)

Last edited by piotrbujakowski; 07-02-2024 at 04:44 PM.
 
Old 06-25-2024, 04:42 PM   #2
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
Blog Entries: 1

Rep: Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886
As a start, find out what is the difference between [quote] and [code]
 
1 members found this post helpful.
Old 06-25-2024, 04:50 PM   #3
astrogeek
Moderator
 
Registered: Oct 2008
Distribution: Slackware [64]-X.{0|1|2|37|-current} ::12<=X<=15, FreeBSD_12{.0|.1}
Posts: 6,295
Blog Entries: 24

Rep: Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254Reputation: 4254
Indeed, please wrap your code and data snippets inside [CODE]...[/CODE] tags as opposed to [QUOTE]...[/QUOTE] tags. Doing so will preserve indentation and provide other visual clues which make it easier for others to comprehend. You may write those yourself as shown, or use the # button available with Advanced edit options. (A complete list of BBCode tags is always available via a link near the bottom of every thread view).

This is especially important when posting code from languages in which whitespace is important, such as Python.
 
1 members found this post helpful.
Old 06-25-2024, 08:01 PM   #4
piotrbujakowski
Member
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 47

Original Poster
Rep: Reputation: 1
Done.
 
Old 06-25-2024, 10:35 PM   #5
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
Blog Entries: 1

Rep: Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886
Maybe you wish to use NeuronGroup?
https://brian2.readthedocs.io/en/sta...uronGroup.html
 
1 members found this post helpful.
Old 06-26-2024, 12:50 AM   #6
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702

Rep: Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535
Code:
 ImportError: cannot import name 'Neuron' from 'brian2' (/usr/lib/python3/dist-packages/brian2/__init__.py)
this is the information you need. Or better to say the issue to solve. Neuron (which is now a python module) is not installed [properly].
So please reinstall it and check if that was really successful (and post the output)
 
1 members found this post helpful.
Old 06-26-2024, 03:43 PM   #7
piotrbujakowski
Member
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 47

Original Poster
Rep: Reputation: 1
I think I need to install from packet downloaded from the website.

Code:
pipx install neuron brain2 --force
Installing to existing venv 'neuron'
  installed package neuron 8.2.4, installed using Python 3.12.3
  These apps are now globally available
    - idraw
    - mkthreadsafe
    - modlunit
    - neurondemo
    - nrngui
    - nrniv
    - nrniv-core
    - nrnivmodl
    - nrnivmodl-core
    - nrnpyenv.sh
    - sortspike
done! ✨ �� ✨
Fatal error from pip prevented installation. Full pip output in file:
    /home/peter/.local/state/pipx/log/cmd_2024-06-26_21.39.14_pip_errors.log

Some possibly relevant errors from pip install:
    ERROR: Could not find a version that satisfies the requirement brain2 (from versions: none)
    ERROR: No matching distribution found for brain2
Code:
sudo apt install python3-brian
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3-brian is already the newest version (2.5.4-4build1).
0 upgraded, 0 newly installed, 0 to remove and 31 not upgraded.


sudo apt-get reinstall python3-brian
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
0 upgraded, 0 newly installed, 1 reinstalled, 0 to remove and 31 not upgraded.
Need to get 0 B/622 kB of archives.
After this operation, 0 B of additional disk space will be used.
(Reading database ... 336660 files and directories currently installed.)
Preparing to unpack .../python3-brian_2.5.4-4build1_all.deb ...
Unpacking python3-brian (2.5.4-4build1) over (2.5.4-4build1) ...
Setting up python3-brian (2.5.4-4build1) ...


python -m pip install matplotlib pytest ipython notebook
Command 'python' not found, did you mean:
  command 'python3' from deb python3
  command 'python' from deb python-is-python3


python3 -m pip install matplotlib pytest ipython notebook
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
    
    See /usr/share/doc/python3.12/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.


python -m pip install brian2tools
Command 'python' not found, did you mean:
  command 'python3' from deb python3
  command 'python' from deb python-is-python3


python3 -m pip install brian2tools
error: externally-managed-environment

× This environment is externally managed
╰─> To install Python packages system-wide, try apt install
    python3-xyz, where xyz is the package you are trying to
    install.
    
    If you wish to install a non-Debian-packaged Python package,
    create a virtual environment using python3 -m venv path/to/venv.
    Then use path/to/venv/bin/python and path/to/venv/bin/pip. Make
    sure you have python3-full installed.
    
    If you wish to install a non-Debian packaged Python application,
    it may be easiest to use pipx install xyz, which will manage a
    virtual environment for you. Make sure you have pipx installed.
    
    See /usr/share/doc/python3.12/README.venv for more information.

note: If you believe this is a mistake, please contact your Python installation or OS distribution provider. You can override this, at the risk of breaking your Python installation or OS, by passing --break-system-packages.
hint: See PEP 668 for the detailed specification.

Last edited by piotrbujakowski; 07-02-2024 at 04:41 PM.
 
Old 06-27-2024, 12:34 AM   #8
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702

Rep: Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535
brain or brian ?
 
2 members found this post helpful.
Old 06-27-2024, 02:11 AM   #9
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
Blog Entries: 1

Rep: Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886
Good question. Brian and Brian2 seems to be actual programs (of course they are incompatible, that goes without saying), Brain is the misspelling.
 
1 members found this post helpful.
Old 06-27-2024, 08:54 AM   #10
piotrbujakowski
Member
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 47

Original Poster
Rep: Reputation: 1
That code works.

Code:
import brian2 as b2
import matplotlib.pyplot as plt

# Neuron parameters
Cm = 1 * b2.ufarad  # membrane capacitance
gl = 5 * b2.nsiemens  # leak conductance
El = -65 * b2.mV  # leak reversal potential
V_th = -50 * b2.mV  # spike threshold
V_reset = -65 * b2.mV  # reset potential after spike

# Neuron model equations
eqs = '''
dv/dt = (gl * (El - v) + I) / Cm : volt
I : amp  # input current
'''

# Create a single neuron
neuron = b2.NeuronGroup(1, eqs, threshold='v > V_th', reset='v = V_reset', method='exact')
neuron.v = El  # initial condition

# Create a constant input current
input_current = 200 * b2.pamp  # amplitude of the input current
neuron.I = input_current

# Record the membrane potential
monitor = b2.StateMonitor(neuron, 'v', record=True)

# Run the simulation for 100 ms
duration = 100 * b2.ms
b2.run(duration)

# Plot the membrane potential over time
plt.figure(figsize=(10, 4))
plt.plot(monitor.t / b2.ms, monitor.v[0] / b2.mV)
plt.xlabel('Time (ms)')
plt.ylabel('Membrane potential (mV)')
plt.title('Membrane potential of a single neuron over time')
plt.grid(True)
plt.show()
Attached Thumbnails
Click image for larger version

Name:	Figure_1.png
Views:	5
Size:	39.8 KB
ID:	43177  
 
Old 06-27-2024, 08:59 AM   #11
pan64
LQ Addict
 
Registered: Mar 2012
Location: Hungary
Distribution: debian/ubuntu/suse ...
Posts: 22,702

Rep: Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535Reputation: 7535
in that case you might want to mark the thread solved.
Also, if you wish to say thanks just click on yes.
 
1 members found this post helpful.
Old 06-27-2024, 05:00 PM   #12
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,323

Rep: Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373Reputation: 5373
EDIT:

I should not have posted anything. My apologies.
 
1 members found this post helpful.
Old 07-01-2024, 09:48 AM   #13
piotrbujakowski
Member
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 47

Original Poster
Rep: Reputation: 1
At that, a moment works.
How make that to stage when I can talk with that?

Edit:

I install that:

Code:
sudo apt-get installpython3-charset-normalizer python3-pyaudio espeak-ng-espeak python3-espeak python3-pykml python3-charset-normalizer qt6-speech-speechd-plugin qt6-speech-flite-plugin qtspeech5-flite-plugin python3-guess-language python3-nltk python3-virtualenv python3-venv python3-charset-normalizer python3-virtualenv
Code:
pipx install SpeechRecognition python3-charset-normalizer pyttsx3
pip always have problems ...

The next stage will be virtualization of the person.

Last edited by piotrbujakowski; 07-02-2024 at 04:41 PM.
 
Old 07-02-2024, 03:53 AM   #14
piotrbujakowski
Member
 
Registered: Jan 2017
Location: London, UK
Distribution: Ubuntu
Posts: 47

Original Poster
Rep: Reputation: 1
I try to use Vosk, but all time is:

Code:
Python 3.12.3 (main, Apr 10 2024, 05:33:47) [GCC 13.2.0] on linux
Type "help", "copyright", "credits" or "license()" for more information.

= RESTART: /home/peter/python/ai_neural_brain_2/engine_brain_2_11_neurons_group_and_synapses_talking.py
ERROR      Brian 2 encountered an unexpected error. If you think this is a bug in Brian 2, please report this issue either to the discourse forum at <http://brian.discourse.group/>, or to the issue tracker at <https://github.com/brian-team/brian2/issues>. Please include this file with debug information in your report: /tmp/brian_debug_vmaxybiq.log  Additionally, you can also include a copy of the script that was run, available at: /tmp/brian_script_pehr7l4x.py Thanks! [brian2]
Traceback (most recent call last):
  File "/usr/lib/python3.12/idlelib/run.py", line 580, in runcode
    exec(code, self.locals)
  File "/home/peter/python/ai_neural_brain_2/engine_brain_2_11_neurons_group_and_synapses_talking.py", line 6, in <module>
    import vosk
ModuleNotFoundError: No module named 'vosk'
I make that:

Code:
source /home/peter/venv/bin/activate
(venv) source /home/peter/venv/bin/activate
(venv) python3 -c "import vosk; print('Vosk successfully imported!')"
bash: !': event not found
(venv) echo $(python3 -c "import vosk; print('Vosk successfully imported!')")
bash: !': event not found
bash: !': event not found
bash: !': event not found
(venv) echo $(python3 -c "import vosk; print('Vosk successfully imported!')")
bash: !': event not found
(venv) python3 -c "import vosk; print('Vosk successfully imported!')
bash: !': event not found
(venv) echo $(python3 -c 'import vosk; print("Vosk successfully imported!")')
Vosk successfully imported!
(venv)
And:

Code:
sudo chmod 775 -R /home/peter/venv/*
sudo chmod 775 -R /home/peter/venv/
sudo chmod +x -R /home/peter/venv/
sudo chmod +x -R /home/peter/venv/*
sudo chown peter -R /home/peter/venv/*
sudo chown peter -R /home/peter/venv/
Nothing change.

Someone can help me with that?

Thanks.

Last edited by piotrbujakowski; 07-03-2024 at 02:41 PM.
 
Old 07-02-2024, 08:02 AM   #15
NevemTeve
Senior Member
 
Registered: Oct 2011
Location: Budapest
Distribution: Debian/GNU/Linux, AIX
Posts: 4,924
Blog Entries: 1

Rep: Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886Reputation: 1886
Use bash command `set +H` to prevent `!` being handled specially.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
I got error while installing python-tk python-psycopg2 python-twisted saili kadam Linux - Newbie 1 09-05-2015 03:03 AM
LXer: Python Python Python (aka Python 3) LXer Syndicated Linux News 0 08-05-2009 08:30 PM
Errors, Errors, and more Errors (KDE 3.4.x GUI Errors) Dralnu Linux - Software 2 05-13-2006 08:30 AM
errors,errors,errors!!! randell6564 Mandriva 2 01-15-2006 02:29 AM
Grub errors...grub errors...grub errors smattmac Linux - Newbie 1 06-13-2005 02:07 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 12:51 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration